package cn.well.cloud.modular.controller;

import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import cn.well.cloud.base.auth.annotion.Permission;
import cn.well.cloud.base.pojo.page.LayuiPageFactory;
import cn.well.cloud.modular.model.CourseInfo;
import cn.well.cloud.modular.model.NoticeLog;
import cn.well.cloud.modular.service.INoticeLogService;
import cn.well.cloud.sys.core.exception.enums.BizExceptionEnum;
import cn.well.cloud.sys.core.log.LogObjectHolder;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 *  课程通知记录控制器
 *
 * @author jiawei
 * @Date 2020-06-04 13:17:33
 */
@Controller
@RequestMapping("/noticeLog")
public class NoticeLogController extends BaseController {

    private String PREFIX = "/modular/course/noticeLog/";

    @Autowired
    private INoticeLogService noticeLogService;

    /**
     * 跳转到 课程通知记录首页
     */
    @RequestMapping("/byCourseId")
    public String indexByCourseId(@RequestParam Long courseInfoId) {
        CourseInfo courseInfo = new CourseInfo();
        courseInfo.setCourseId(courseInfoId);
        LogObjectHolder.me().set(courseInfo);
        return PREFIX + "noticeLog.html";
    }

    /**
     * 跳转到 课程通知记录首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "noticeLog.html";
    }
    /**
     * 跳转到添加 课程通知记录
     */
    @RequestMapping("/noticeLog_add")
    public String noticeLogAdd() {
        return PREFIX + "noticeLog_add.html";
    }

    /**
     * 跳转到编辑 课程通知记录
     */
    @RequestMapping("/noticeLog_edit")
    public String noticeLogEdit(@RequestParam Long noticeLogId) {
		if (ToolUtil.isEmpty(noticeLogId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        NoticeLog noticeLog = noticeLogService.getById(noticeLogId);
        LogObjectHolder.me().set(noticeLog);
        return PREFIX + "noticeLog_edit.html";
    }

    /**
     * 获取 课程通知记录列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(value = "courseInfoId", required = false) String courseInfoId
                            , @RequestParam(value = "wxType", required = false) String wxType
                            ,@RequestParam(value = "searchName", required = false) String searchName) {
    
    	Page page = LayuiPageFactory.defaultPage();
        IPage<Map<String, Object>> pageInfo = noticeLogService.pageMapsInfos(page, courseInfoId,wxType,searchName);

        return LayuiPageFactory.createPageInfo(pageInfo);
    }

    /**
     * 添加 课程通知记录
     */
    @Permission
	@RequestMapping(value = "/add")
    @ResponseBody
    public Object add(NoticeLog noticeLog) {
        noticeLogService.save(noticeLog);
        return BaseController.SUCCESS_TIP;
    }

    /**
     * 删除 课程通知记录
     */
    @Permission
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long noticeLogId) {
        noticeLogService.removeById(noticeLogId);
        return BaseController.SUCCESS_TIP;
    }

    /**
     * 修改 课程通知记录
     */
    @Permission
    @RequestMapping(value = "/edit")
    @ResponseBody
    public Object edit(NoticeLog noticeLog) {
        noticeLogService.updateById(noticeLog);
        return BaseController.SUCCESS_TIP;
    }

    /**
     *  课程通知记录详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail(@RequestParam("noticeLogId") Long noticeLogId) {
    	NoticeLog noticeLog = noticeLogService.getById(noticeLogId);
    	return ResponseData.success(noticeLog);
    }
}
